package com.tech.mapper.tem.imp;

import com.tech.mapper.GoodsSortDao;
import com.tech.repository.entity.GoodsSort;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class GoodsSortDaoMysqlImp extends BasicDaoImp<GoodsSort> implements GoodsSortDao {
	@Override
	public GoodsSort getGoodsSort(int id) {
		return null;
	}

	@Override
	public GoodsSort getParentGoodsSort(int id) {
		return null;
	}

	@Override
	public List<GoodsSort> getChildGoodsSorts(int id) {
		return null;
	}

	@Override
	public List<GoodsSort> getChildGoodsSorts(String sortName) {
		SqlTask<String, List<GoodsSort>> sqlTask = (item, data) -> {
			String sql = "select g.* from goodssort left join goodssort g on g.parentid = goodssort.sid where goodssort.ssortname = ? and g.sid is not null;";
			ArrayList<GoodsSort> list = new ArrayList<>();
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setString(1, data);
			ResultSet rs = item.executeQuery();
			while (rs.next()) {
				list.add(getInstance(rs));
			}
			return list;
		};
		return SqlTask.processSql(sqlTask, sortName);
	}

	@Override
	public TreeMap<Integer, List<GoodsSort>> getAllGoodsSorts() {
		return null;
	}

	@Override
	public int addGoodsSort(GoodsSort GoodsSort) {
		return 0;
	}

	@Override
	public int[] addGoodsSorts(List<GoodsSort> GoodsSortList) {
		return new int[0];
	}

	@Override
	public boolean delGoodsSort(int id) {
		return false;
	}

	@Override
	public int[] delGoodsSorts(List<Integer> GoodsSortsList) {
		return new int[0];
	}

	@Override
	public boolean delAllGoodsSorts() {
		return false;
	}

	@Override
	protected GoodsSort getInstance(ResultSet rs) throws SQLException {
		return new GoodsSort(rs.getInt("sid"),
				rs.getInt("parentid"),
				rs.getString("ssortname"),
				rs.getInt("parentid") >= 1 ? GoodsSort.SortRank.SMALL_SORT : GoodsSort.SortRank.BIG_SORT);
	}
}
